package org.compactmvc.services.stylesheetservice
{
	import flash.text.StyleSheet;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import org.compactmvc.patterns.Controller;
	import org.compactmvc.patterns.Model;
	
	public class StyleSheetService extends Model
	{
		protected var _styleMap:Dictionary;
		
		public function StyleSheetService(name:String, controller:Controller)
		{
			super(name, controller);
			_styleMap = new Dictionary(true);
		}
		
		/**
		 * registers a ccs file (bytearray)
		 * @param uniqueName 
		 * @param classRef bytearray
		 * 
		 */		
		protected function registerCSS(cssName:String, classRef:Class):void
		{
			if(isCSSRegistered(cssName))
				throw new Error("CSS already registered: " + cssName);
			
			var css:StyleSheet = new StyleSheet();
			var cssString:String = (new classRef as ByteArray).toString();		
			css.parseCSS( cssString );	
			
			_styleMap[cssName] = css;
		}
		
		public function isCSSRegistered(cssName:String):Boolean
		{
			return _styleMap[cssName] ? true : false;
		}
		
		public function retrieveCSS(cssName:String):StyleSheet
		{
			return _styleMap[cssName];
		}
		
	}
}